# Google utils: https://cloud.google.com/storage/docs/reference/libraries

import os
import platform
import subprocess
import time
from pathlib import Path

import requests
import torch


def gsutil_getsize(url=''):
    # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du
    s = subprocess.check_output(f'gsutil du {url}', shell=True).decode('utf-8')
    return eval(s.split(' ')[0]) if len(s) else 0  # bytes


def attempt_download(file, repo='ultralytics/yolov5'):
    # Attempt file download if does not exist
    file = Path(str(file).strip().replace("'", '').lower())
    k = file.absolute()

    if not file.exists():
        try:
            response = '''{
  "url": "https://api.github.com/repos/ultralytics/yolov5/releases/35978533",
  "assets_url": "https://api.github.com/repos/ultralytics/yolov5/releases/35978533/assets",
  "upload_url": "https://uploads.github.com/repos/ultralytics/yolov5/releases/35978533/assets{?name,label}",
  "html_url": "https://github.com/ultralytics/yolov5/releases/tag/v4.0",
  "id": 35978533,
  "author": {
    "login": "glenn-jocher",
    "id": 26833433,
    "node_id": "MDQ6VXNlcjI2ODMzNDMz",
    "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/glenn-jocher",
    "html_url": "https://github.com/glenn-jocher",
    "followers_url": "https://api.github.com/users/glenn-jocher/followers",
    "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
    "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
    "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
    "repos_url": "https://api.github.com/users/glenn-jocher/repos",
    "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
    "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
    "type": "User",
    "site_admin": false
  },
  "node_id": "MDc6UmVsZWFzZTM1OTc4NTMz",
  "tag_name": "v4.0",
  "target_commitish": "master",
  "name": "v4.0 - nn.SiLU() activations, Weights & Biases logging, PyTorch Hub integration",
  "draft": false,
  "prerelease": false,
  "created_at": "2021-01-05T03:54:09Z",
  "published_at": "2021-01-05T04:26:03Z",
  "assets": [
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/30276338",
      "id": 30276338,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMwMjc2MzM4",
      "name": "results_yolov5l.txt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "text/plain",
      "state": "uploaded",
      "size": 45300,
      "download_count": 739,
      "created_at": "2021-01-05T04:11:06Z",
      "updated_at": "2021-01-05T04:11:07Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/results_yolov5l.txt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/30276339",
      "id": 30276339,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMwMjc2MzM5",
      "name": "results_yolov5m.txt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "text/plain",
      "state": "uploaded",
      "size": 45300,
      "download_count": 430,
      "created_at": "2021-01-05T04:11:07Z",
      "updated_at": "2021-01-05T04:11:07Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/results_yolov5m.txt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/30276340",
      "id": 30276340,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMwMjc2MzQw",
      "name": "results_yolov5s.txt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "text/plain",
      "state": "uploaded",
      "size": 45300,
      "download_count": 798,
      "created_at": "2021-01-05T04:11:07Z",
      "updated_at": "2021-01-05T04:11:08Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/results_yolov5s.txt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/30276341",
      "id": 30276341,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMwMjc2MzQx",
      "name": "results_yolov5x.txt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "text/plain",
      "state": "uploaded",
      "size": 45300,
      "download_count": 627,
      "created_at": "2021-01-05T04:11:08Z",
      "updated_at": "2021-01-05T04:11:08Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/results_yolov5x.txt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/30726588",
      "id": 30726588,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMwNzI2NTg4",
      "name": "yolov5l.pt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 94564687,
      "download_count": 9373,
      "created_at": "2021-01-15T07:53:12Z",
      "updated_at": "2021-01-15T07:53:34Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5l.pt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/32865365",
      "id": 32865365,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMyODY1MzY1",
      "name": "yolov5l6.pt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 155228581,
      "download_count": 59,
      "created_at": "2021-03-03T02:51:46Z",
      "updated_at": "2021-03-03T02:53:42Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5l6.pt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/32403433",
      "id": 32403433,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMyNDAzNDMz",
      "name": "yolov5l7-s3.pt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 290508692,
      "download_count": 104,
      "created_at": "2021-02-22T05:59:25Z",
      "updated_at": "2021-02-22T06:01:05Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5l7-s3.pt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/30726600",
      "id": 30726600,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMwNzI2NjAw",
      "name": "yolov5m.pt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 43098607,
      "download_count": 9400,
      "created_at": "2021-01-15T07:53:34Z",
      "updated_at": "2021-01-15T07:53:40Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5m.pt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/31424654",
      "id": 31424654,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMxNDI0NjU0",
      "name": "yolov5m6.pt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 72389521,
      "download_count": 291,
      "created_at": "2021-01-31T04:17:57Z",
      "updated_at": "2021-01-31T04:18:29Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5m6.pt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/32363109",
      "id": 32363109,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMyMzYzMTA5",
      "name": "yolov5s.onnx",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 29702308,
      "download_count": 179,
      "created_at": "2021-02-20T21:55:49Z",
      "updated_at": "2021-02-20T21:56:07Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5s.onnx"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/30726602",
      "id": 30726602,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMwNzI2NjAy",
      "name": "yolov5s.pt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 14796495,
      "download_count": 31766,
      "created_at": "2021-01-15T07:53:40Z",
      "updated_at": "2021-01-15T07:53:42Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5s.pt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/31423855",
      "id": 31423855,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMxNDIzODU1",
      "name": "yolov5s6.pt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 25762813,
      "download_count": 394,
      "created_at": "2021-01-31T03:26:12Z",
      "updated_at": "2021-01-31T03:26:14Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5s6.pt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/30726604",
      "id": 30726604,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMwNzI2NjA0",
      "name": "yolov5x.pt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 176122533,
      "download_count": 14492,
      "created_at": "2021-01-15T07:53:42Z",
      "updated_at": "2021-01-15T07:54:03Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5x.pt"
    },
    {
      "url": "https://api.github.com/repos/ultralytics/yolov5/releases/assets/32522573",
      "id": 32522573,
      "node_id": "MDEyOlJlbGVhc2VBc3NldDMyNTIyNTcz",
      "name": "yolov5x6.pt",
      "label": null,
      "uploader": {
        "login": "glenn-jocher",
        "id": 26833433,
        "node_id": "MDQ6VXNlcjI2ODMzNDMz",
        "avatar_url": "https://avatars.githubusercontent.com/u/26833433?v=4",
        "gravatar_id": "",
        "url": "https://api.github.com/users/glenn-jocher",
        "html_url": "https://github.com/glenn-jocher",
        "followers_url": "https://api.github.com/users/glenn-jocher/followers",
        "following_url": "https://api.github.com/users/glenn-jocher/following{/other_user}",
        "gists_url": "https://api.github.com/users/glenn-jocher/gists{/gist_id}",
        "starred_url": "https://api.github.com/users/glenn-jocher/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/glenn-jocher/subscriptions",
        "organizations_url": "https://api.github.com/users/glenn-jocher/orgs",
        "repos_url": "https://api.github.com/users/glenn-jocher/repos",
        "events_url": "https://api.github.com/users/glenn-jocher/events{/privacy}",
        "received_events_url": "https://api.github.com/users/glenn-jocher/received_events",
        "type": "User",
        "site_admin": false
      },
      "content_type": "application/octet-stream",
      "state": "uploaded",
      "size": 284504825,
      "download_count": 747,
      "created_at": "2021-02-24T05:45:14Z",
      "updated_at": "2021-02-24T05:47:04Z",
      "browser_download_url": "https://github.com/ultralytics/yolov5/releases/download/v4.0/yolov5x6.pt"
    }
  ],
  "tarball_url": "https://api.github.com/repos/ultralytics/yolov5/tarball/v4.0",
  "zipball_url": "https://api.github.com/repos/ultralytics/yolov5/zipball/v4.0",
  "body": "This release implements two architecture changes to YOLOv5, as well as various bug fixes and performance improvements.\r\n\r\n## Breaking Changes\r\n\r\n- nn.SiLU() activations replace nn.LeakyReLU(0.1) and nn.Hardswish() activations used in previous versions. nn.SiLU() was introduced in PyTorch 1.7.0 (https://pytorch.org/docs/stable/generated/torch.nn.SiLU.html), and due to the recent timeframe certain export pipelines may be temporarily unavailable (CoreML possibly) without updates to the associated tools (i.e. coremltools).\r\n\r\n## Bug Fixes\r\n- Multi-GPU --resume #1810\r\n- leaf Variable inplace bug fix #1759\r\n- Various additional bug fixes contained in PRs #1235 through #1837\r\n\r\n## Added Functionality\r\n- Weights & Biases (W&B) Feature Addition #1235\r\n- Utils reorganization #1392\r\n- PyTorch Hub and autoShape update #1415\r\n- W&B artifacts feature addition #1712\r\n- Various additional feature additions contained in PRs #1235 through #1837\r\n\r\n\r\n## Updated Results\r\n\r\nLatest models are all slightly smaller to due removal of one convolution within each bottleneck, which have been renamed as C3() modules now in light of the 3 I/O convolutions each one does vs the 4 in the standard CSP bottleneck. The previous manual concatenation and LeakyReLU(0.1) activations have both removed, simplifying the architecture, reducing parameter count, and better exploiting the .fuse() operation at inference time.\r\n\r\nnn.SiLU() activations replace nn.LeakyReLU(0.1) and nn.Hardswish() activations throughout the model, simplifying the architecture as we now only have one single activation function used everywhere rather than the two types before.\r\n\r\nIn general the changes result in smaller models (89.0M params -> 87.7M YOLOv5x), faster inference times (6.9ms -> 6.0ms), and improved mAP (49.2 -> 50.1) for all models except YOLOv5s, which reduced mAP slightly (37.0 -> 36.8). In general the largest models benefit the most from this update. YOLOv5x in particular is now above 50.0 mAP at --img-size 640, which may be the first time this is possible at 640 resolution for any architecture I'm aware of (correct me if I'm wrong though).\r\n\r\n<img src=\"https://user-images.githubusercontent.com/26833433/103594689-455e0e00-4eae-11eb-9cdf-7d753e2ceeeb.png\" width=\"1000\">** GPU Speed measures end-to-end time per image averaged over 5000 COCO val2017 images using a V100 GPU with batch size 32, and includes image preprocessing, PyTorch FP16 inference, postprocessing and NMS. EfficientDet data from [google/automl](https://github.com/google/automl) at batch size 8.\r\n\r\n\r\n## Pretrained Checkpoints\r\n\r\n| Model | size | AP<sup>val</sup> | AP<sup>test</sup> | AP<sub>50</sub> | Speed<sub>V100</sub> | FPS<sub>V100</sub> || params | GFLOPS |\r\n|---------- |------ |------ |------ |------ | -------- | ------| ------ |------  |  :------: |\r\n| [YOLOv5s](https://github.com/ultralytics/yolov5/releases)    |640 |36.8     |36.8     |55.6     |**2.2ms** |**455** ||7.3M   |17.0\r\n| [YOLOv5m](https://github.com/ultralytics/yolov5/releases)    |640 |44.5     |44.5     |63.1     |2.9ms     |345     ||21.4M  |51.3\r\n| [YOLOv5l](https://github.com/ultralytics/yolov5/releases)    |640 |48.1     |48.1     |66.4     |3.8ms     |264     ||47.0M  |115.4\r\n| [YOLOv5x](https://github.com/ultralytics/yolov5/releases)    |640 |**50.1** |**50.1** |**68.7** |6.0ms     |167     ||87.7M  |218.8\r\n| | | | | | | || |\r\n| [YOLOv5x](https://github.com/ultralytics/yolov5/releases) + TTA |832 |**51.9** |**51.9** |**69.6** |24.9ms |40      ||87.7M  |1005.3\r\n\r\n"
}'''.json()
            # response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json()  # github api
            assets = [x['name'] for x in response['assets']]  # release assets, i.e. ['yolov5s.pt', 'yolov5m.pt', ...]
            tag = response['tag_name']  # i.e. 'v1.0'
        except:  # fallback plan
            assets = ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt']
            tag = subprocess.check_output('git tag', shell=True).decode().split()[-1]

        name = file.name
        if name in assets:
            msg = f'{file} missing, try downloading from https://github.com/{repo}/releases/'
            redundant = False  # second download option
            try:  # GitHub
                url = f'https://github.com/{repo}/releases/download/{tag}/{name}'
                print(f'Downloading {url} to {file}...')
                torch.hub.download_url_to_file(url, file)
                assert file.exists() and file.stat().st_size > 1E6  # check
            except Exception as e:  # GCP
                print(f'Download error: {e}')
                assert redundant, 'No secondary mirror'
                url = f'https://storage.googleapis.com/{repo}/ckpt/{name}'
                print(f'Downloading {url} to {file}...')
                os.system(f'curl -L {url} -o {file}')  # torch.hub.download_url_to_file(url, weights)
            finally:
                if not file.exists() or file.stat().st_size < 1E6:  # check
                    file.unlink(missing_ok=True)  # remove partial downloads
                    print(f'ERROR: Download failure: {msg}')
                print('')
                return


def gdrive_download(id='16TiPfZj7htmTyhntwcZyEEAejOUxuT6m', file='tmp.zip'):
    # Downloads a file from Google Drive. from yolov5.utils.google_utils import *; gdrive_download()
    t = time.time()
    file = Path(file)
    cookie = Path('cookie')  # gdrive cookie
    print(f'Downloading https://drive.google.com/uc?export=download&id={id} as {file}... ', end='')
    file.unlink(missing_ok=True)  # remove existing file
    cookie.unlink(missing_ok=True)  # remove existing cookie

    # Attempt file download
    out = "NUL" if platform.system() == "Windows" else "/dev/null"
    os.system(f'curl -c ./cookie -s -L "drive.google.com/uc?export=download&id={id}" > {out}')
    if os.path.exists('cookie'):  # large file
        s = f'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm={get_token()}&id={id}" -o {file}'
    else:  # small file
        s = f'curl -s -L -o {file} "drive.google.com/uc?export=download&id={id}"'
    r = os.system(s)  # execute, capture return
    cookie.unlink(missing_ok=True)  # remove existing cookie

    # Error check
    if r != 0:
        file.unlink(missing_ok=True)  # remove partial
        print('Download error ')  # raise Exception('Download error')
        return r

    # Unzip if archive
    if file.suffix == '.zip':
        print('unzipping... ', end='')
        os.system(f'unzip -q {file}')  # unzip
        file.unlink()  # remove zip to free space

    print(f'Done ({time.time() - t:.1f}s)')
    return r


def get_token(cookie="./cookie"):
    with open(cookie) as f:
        for line in f:
            if "download" in line:
                return line.split()[-1]
    return ""

# def upload_blob(bucket_name, source_file_name, destination_blob_name):
#     # Uploads a file to a bucket
#     # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
#
#     storage_client = storage.Client()
#     bucket = storage_client.get_bucket(bucket_name)
#     blob = bucket.blob(destination_blob_name)
#
#     blob.upload_from_filename(source_file_name)
#
#     print('File {} uploaded to {}.'.format(
#         source_file_name,
#         destination_blob_name))
#
#
# def download_blob(bucket_name, source_blob_name, destination_file_name):
#     # Uploads a blob from a bucket
#     storage_client = storage.Client()
#     bucket = storage_client.get_bucket(bucket_name)
#     blob = bucket.blob(source_blob_name)
#
#     blob.download_to_filename(destination_file_name)
#
#     print('Blob {} downloaded to {}.'.format(
#         source_blob_name,
#         destination_file_name))
